import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 带字符计数的文本输入框
/// 
/// 特点：
/// - 灰色背景
/// - 右下角显示字符计数（如 "120/140"）
/// - 支持自定义最大字符数、最大行数、提示文本等
class CharacterCountTextField extends StatefulWidget {
  /// 文本控制器
  final TextEditingController controller;

  /// 提示文本
  final String? hintText;

  /// 最大字符数
  final int maxLength;

  /// 最大行数
  final int maxLines;

  /// 最小高度
  final double? minHeight;

  /// 背景颜色
  final Color? backgroundColor;

  /// 字符计数颜色
  final Color? counterColor;

  /// 字符计数文本样式
  final TextStyle? counterTextStyle;

  /// 输入框内容变化回调
  final ValueChanged<String>? onChanged;

  /// 提交回调
  final ValueChanged<String>? onSubmitted;

  /// 是否显示键盘确认键（true显示确认键，false显示换行键）
  final bool showDoneAction;

  const CharacterCountTextField({
    super.key,
    required this.controller,
    this.hintText,
    this.maxLength = 140,
    this.maxLines = 5,
    this.minHeight,
    this.backgroundColor,
    this.counterColor,
    this.counterTextStyle,
    this.onChanged,
    this.onSubmitted,
    this.showDoneAction = true,
  });

  @override
  State<CharacterCountTextField> createState() =>
      _CharacterCountTextFieldState();
}

class _CharacterCountTextFieldState extends State<CharacterCountTextField> {
  @override
  Widget build(BuildContext context) {
    final backgroundColor = widget.backgroundColor ??
        const Color(0xFFF5F5F5);
    final counterColor = widget.counterColor ??
        context.customColorThemeNonNull.textColor2;

    return Container(
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(8.w),
      ),
      constraints: widget.minHeight != null
          ? BoxConstraints(minHeight: widget.minHeight!)
          : null,
      child: Stack(
        children: [
          TextField(
            controller: widget.controller,
            maxLength: widget.maxLength,
            maxLengthEnforcement: MaxLengthEnforcement.enforced,
            maxLines: widget.maxLines,
            textInputAction: widget.showDoneAction
                ? TextInputAction.done
                : TextInputAction.newline,
            decoration: InputDecoration(
              hintText: widget.hintText,
              hintStyle: context.textTheme.bodyMedium?.copyWith(
                color: context.customColorThemeNonNull.textColor2,
              ),
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(
                left: 12.w,
                top: 12.h,
                right: 12.w,
                bottom: 28.h, // 为字符计数留出空间
              ),
              counterText: '', // 隐藏默认计数器
            ),
            style: context.textTheme.bodyMedium?.copyWith(
              color: context.customColorThemeNonNull.textColor1,
            ),
            onChanged: (value) {
              setState(() {
                // 更新字符计数（通过 setState 触发 rebuild）
              });
              widget.onChanged?.call(value);
            },
            onSubmitted: (value) {
              if (widget.showDoneAction) {
                // 点击确认键时收回键盘
                FocusScope.of(context).unfocus();
              }
              widget.onSubmitted?.call(value);
            },
          ),
          // 自定义字符计数显示在右下角
          Positioned(
            bottom: 8.h,
            right: 12.w,
            child: Text(
              '${widget.controller.text.length}/${widget.maxLength}',
              style: widget.counterTextStyle ??
                  context.textTheme.bodySmall?.copyWith(
                    color: counterColor,
                  ),
            ),
          ),
        ],
      ),
    );
  }
}

